/*******************************************************************************
*  file    : transform_change_freq.cpp
*  created : 27.06.2014
*  author  : Slyshyk Oleksiy (alexSlyshyk@gmail.com)
*******************************************************************************/

#include "transform_change_freq_int.hpp"
#include <QDebug>
#include <algorithm>
#include <QVariantMap>

#include <utils.hpp>


TransformChangeFreq::TransformChangeFreq(QObject *parent) :
    QObject(parent)
{

}

QString TransformChangeFreq::name() const
{
    return QStringLiteral("Change Freq Int");
}

NamedIntData *TransformChangeFreq::transform(const NamedIntData *data, const QVariant &params)
{
    QScopedPointer<plugin_utils::TransformParamsPrivate> data_;
    data_.reset(new plugin_utils::TransformParamsPrivate );
    data_->setParams(params);

    NamedIntData* res = new NamedIntData;
    res->name = data->name + "_ch_freq_int";
    res->pantry["id"] = params.toMap()["id"];

    transform(data->data, data, res, data_.data());

    return res;

    Q_UNUSED(params);
}

QStringList TransformChangeFreq::patternList() const
{
    return plugin_utils::fileList(QStringLiteral("pattern*.dat"));
}

void TransformChangeFreq::transform(const std::vector<int32_t> &sample,
                                    const NamedIntData *data, NamedIntData *res,
                                    plugin_utils::TransformParamsPrivate *data_)
{
    if(data_->freqCoef() == 0)
        data_->setSampleRate(1.0);

    res->sampleRate = data->sampleRate * data_->freqCoef();
    res->data.resize( data->data.size() * data_->freqCoef());

    int32_t start_time = 0;
    int32_t dt = 1 ;
    int32_t dt_data = 1 * data_->freqCoef();

    const auto &dat = data->data;
    double point_time;
    int32_t iter_count = 0 ;

    if(data_->smooth().contains("linear"))
        for(int32_t idx_data = 0; idx_data < static_cast<int32_t>(res->data.size()) ; ++idx_data)
            {
                point_time = (idx_data)*(1.0 / res->sampleRate);
                if(point_time < data_->zoneBegin() || point_time > data_->zoneEnd())
                    res->data[idx_data] = 0;
                else
                    {
                        res->data[idx_data] = plugin_utils::get_func_value_linear(dat,
                                                                                  start_time,
                                                                                  dt_data,
                                                                                  idx_data * dt );
                        iter_count++ ;
                    }

            }
    else if(data_->smooth().contains("square"))
        for(int32_t idx_data = 0; idx_data < static_cast<int32_t>(res->data.size()) ; ++idx_data)
            {
                point_time = (idx_data)*(1.0 / res->sampleRate);
                if(point_time < data_->zoneBegin() || point_time > data_->zoneEnd())
                    res->data[idx_data] = 0;
                else
                    {
                        res->data[idx_data] = plugin_utils::get_func_value_square(dat,
                                                                                  start_time,
                                                                                  dt_data,
                                                                                  idx_data * dt);
                        iter_count++ ;
                    }

            }


    res->pantry["iter_count"] = iter_count ;
    qDebug() << "return data zise = " << res->data.size();
    Q_UNUSED(sample)
}
